package TankFrst;

import java.awt.*;
import java.util.List;

public class Missile {
    public static final int XSPREED=10;
    public static final int YSPREED=10;

    public static final int MISSILEHIGH=10;
    public static final int MISSILEWIGTH=10;
    int x,y;
    Dir dir=Dir.R;//设置方向
    private boolean live=true;
    private boolean good;
    private TankClient tc;

//    public boolean isGood() {
//        return good;
//    }

//    public void setGood(boolean good) {
//        this.good = good;
//    }

    public Missile(int x, int y, Dir dir) {
        this.x = x;
        this.y = y;
        this.dir = dir;
    }

    public Missile(int x, int y, boolean good,Dir dir, TankClient tc) {
        this(x,y,dir);
        this.good=good;
        this.tc = tc;
    }
//呼出子弹的大小和颜色
    public void draw(Graphics g){
        if (!live) return;
        Color c=g.getColor();
        if (this.good)
        g.setColor(Color.black);
        else g.setColor(Color.RED);
        g.fillOval(x,y,MISSILEWIGTH,MISSILEHIGH);
        g.setColor(c);
        move();
    }
//与坦克
    private void move() {
        switch (dir) {
            case L:
                x -= XSPREED;
                break;
            case LU:
                x -= XSPREED;
                y -= YSPREED;
                break;
            case U:
                y -= YSPREED;
                break;
            case RU:
                x += XSPREED;
                y -= YSPREED;
                break;
            case R:
                x += XSPREED;
                break;
            case RD:
                x += XSPREED;
                y += YSPREED;
                break;
            case D:
                y += YSPREED;
                break;
            case LD:
                x -= XSPREED;
                y += YSPREED;
                break;
        }
        //判断子弹是否出界
        if (x<0||x> TankClient.GAME_WEIGHT||y<0||y> TankClient.GAME_HIGH){
            live=false;
            tc.missiles.remove(this);
        }
    }

    public boolean isLive() {
        return live;
    }

    public Rectangle getRect(){
        return new Rectangle(x,y, Tank.TANKWIGTH, Tank.TANKHIGH);
    }
    //打击Tank,判断矩形是否相撞
    public boolean hitTank(Tank t){
        if (this.live&&this.getRect().intersects(t.getRect())&&t.isLive()&&this.good!=t.isGood()){
//            if (t.isGood()){
//                t.setLife(t.getLife()-20);
//                this.live=false;
//                if (t.getLife()<=0)
//                {
//                    t.setLive(false);
//                }
//            }else {
//                t.setLive(false);
//            }
            t.setLive(false);
            Explode e=new Explode(tc,x,y);
            tc.explodes.add(e);
            return true;
        }
        return false;
    }
//打击敌方坦克
    public boolean hitTanks(List<Tank> tanks){
        for (int i = 0; i < tanks.size(); i++) {
            if (hitTank(tanks.get(i))){
                return true;
            }
        }
        return false;
    }

}
